from django.utils.deprecation import MiddlewareMixin
from meiduo.settings import SECRET_KEY
import jwt
class JWTMiddleware(MiddlewareMixin):

    def process_request(self, request):
        # 拦截所有的请求 对令牌进行解析 并得出当前访问的用户的身份(uid)
        token = request.GET.get("token")
        # 解析这个令牌
        try:
            data = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
            uid = data.get("uid")
        except Exception as e:
            print(e)
            uid = None
        # request.POST["uid"] = uid
        request.COOKIES["uid"] = uid